const cmd = require("node-cmd");


module.exports = {
    help(){
        console.log('获取当前页面包名：adb shell dumpsys window | findstr mCurrentFocus')
    },
    connect(){
        cmd.run("adb devices",function(err,data){
            console.log(data);
        });
    },
    startApp(packageName){
        cmd.run(`adb shell monkey -p ${packageName} 1`,function(err,data){
            err &&  console.log(err);
        });
    },
    closeApp(packageName){
        cmd.run(`adb shell am force-stop ${packageName} `,function(err,data){
            err &&  console.log(err);
        });
    },
    sleep(time = 0) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve();
            }, time);
        })
    },
    input(text){
        cmd.run(`adb shell input text  ${text} `,function(err,data){
            err &&  console.log(err);
        });
    },
    tap(x,y){
        cmd.run(`adb shell input tap ${x} ${y} `,function(err,data){
            err &&  console.log(err);
        });
    },
    swipe({x,y},{x2,y2},time){
        cmd.run(`adb shell input swipe ${x} ${y} ${x2} ${y2} ${time}`,function(err,data){
            err &&  console.log(err);
        });
    },
    screen(file = 'temp.png',dir = 'tmp/'){
        cmd.run(`adb shell screencap /sdcard/${file}.png`);
        cmd.run(`adb pull /sdcard/${file}.png ./${dir}`);
        cmd.run(`adb shell rm /sdcard/${file}.png`);
    },
    keyevent(key=4){
        cmd.run(`adb shell input keyevent ${key}`,function(err,data){
            err &&  console.log(err);
        });  
    }

}